package com.example.myapplication;

import android.app.Application;
import android.content.SharedPreferences;
import android.util.TypedValue;

import com.baidu.mapapi.SDKInitializer;
import com.example.myapplication.view.PullRefreshViewHelper;

public class MyApp extends Application {
    private static MyApp instance = null;

    public static MyApp get() {
        return instance;
    }

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        PullRefreshViewHelper.get().init(14);
        PullRefreshViewHelper.get().setColors(getColors(R.color.purple_200), getColors(R.color.purple_200));
        PullRefreshViewHelper.get().setLoadStyle(PullRefreshViewHelper.LOAD_STYLE_FADING);
        PullRefreshViewHelper.get().setLoadColor(getColors(R.color.white));
        PullRefreshViewHelper.get().setLoadText("加载中...");
        PullRefreshViewHelper.get().setEmptyImage(R.mipmap.ic_pull_refresh);
        PullRefreshViewHelper.get().setEmptyColor(getColors(R.color.teal_200));
        PullRefreshViewHelper.get().setEmptyText("暂无数据~");
        PullRefreshViewHelper.get().setErrorImage(R.mipmap.ic_pull_refresh);
        PullRefreshViewHelper.get().setErrorColor(getColors(R.color.teal_200));
        PullRefreshViewHelper.get().setErrorText("加载失败了~");


        //sp数据存储代码
        sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        SDKInitializer.initialize(this);
    }

    public SharedPreferences getSharedPreferences() {
        return sharedPreferences;
    }
    public SharedPreferences.Editor getEditor() {
        return editor;
    }


    //----------------------------------------------------------------------------------------------------------------------------------------
    public int getColors(int id) {
        return getResources().getColor(id);
    }

    public int getWidth() {
        return getResources().getDisplayMetrics().widthPixels;
    }

    public int getHeight() {
        return getResources().getDisplayMetrics().heightPixels;
    }

    public int dp2Sp(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dp, getResources().getDisplayMetrics());
    }

    public int dp2Px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }
}
